using Headspring.Training.Domain.Services;

namespace Headspring.Training.Domain.Model.StateCommands
{
	public abstract class StateCommandBase : IStateCommand
	{
		protected WorkOrder _workOrder;
		protected Employee _currentUser;

		protected StateCommandBase(WorkOrder workOrder, Employee currentUser)
		{
			_workOrder = workOrder;
			_currentUser = currentUser;
		}

	  public abstract WorkOrderStatus GetBeginStatus();
		protected abstract WorkOrderStatus GetEndStatus();
		protected abstract bool userCanExecute(Employee currentUser);
		public abstract string TransitionVerbPresentTense { get; }
		public abstract string TransitionVerbPastTense { get; }
		protected abstract void postExecute(IWorkflowCoordinator coordinator);

		public bool IsValid()
		{
			bool beginStatusMatches = _workOrder.Status == GetBeginStatus();
			bool currentUserIsCorrectRole = userCanExecute(_currentUser);
			return beginStatusMatches && currentUserIsCorrectRole;
		}

		public void Execute(IWorkflowCoordinator coordinator)
		{
			_workOrder.Status = GetEndStatus();
			coordinator.SaveWorkOrder(_workOrder);
			string message = string.Format("You have {0} work order {1}", TransitionVerbPastTense.ToLower(), _workOrder.Number);
			coordinator.SendMessage(message);
			postExecute(coordinator);
		}

		public bool Matches(string commandName)
		{
			return TransitionVerbPresentTense == commandName;
		}
	}
}